/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.trigger;

import java.util.Date;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Scope;

import com.fujitsu.nanjing.tieba.service.AttachService;
import com.fujitsu.nanjing.tieba.service.BoardService;
import com.fujitsu.nanjing.tieba.service.ReplyService;
import com.fujitsu.nanjing.tieba.service.TopicService;
import com.fujitsu.nanjing.tieba.service.UserService;
import com.fujitsu.nanjing.tieba.view.velocity.tools.DomainTool;

/**
 * BaseinfoTrigger.java
 * 
 * @author Administrator
 */
@Configurable("baseinfoTrigger")
@Scope
@DependsOn
public class BaseinfoTrigger implements Job {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(BaseinfoTrigger.class);

	private UserService userService;
	private AttachService attachService;
	private BoardService boardService;
	private TopicService topicService;
	private ReplyService replyService;
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public AttachService getAttachService() {
		return attachService;
	}

	public void setAttachService(AttachService attachService) {
		this.attachService = attachService;
	}

	public BoardService getBoardService() {
		return boardService;
	}

	public void setBoardService(BoardService boardService) {
		this.boardService = boardService;
	}

	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		perform();
		logger.info("BaseInfoTrigger执行！" + new Date().toString());
	}
	@SuppressWarnings("deprecation")
	private void perform() {
		DomainTool domainTool = new DomainTool();
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("boards", String.valueOf(boardService.countBoards()));
		map.put("topics", String.valueOf(topicService.count()));
		map.put("digests", String.valueOf(topicService.countDigists()));
		map.put("replies", String.valueOf(replyService.countReplies()));
		map.put("attachs", String.valueOf(attachService.countAttachs()));
		map.put("users", String.valueOf(userService.countUsers()));
		map.put("avgReplies",
				String.valueOf(Integer.parseInt(map.get("replies"))
						/ Integer.parseInt(map.get("users"))));
		map.put("topicUsers", String.valueOf(userService.countTopicUsers()));
		map.put("admins", String.valueOf(userService.countAdmins()));
		map.put("avgTopics",
				String.valueOf(Integer.parseInt(map.get("topics"))
						/ Integer.parseInt(map.get("users"))));
		map.put("refreshTime", new Date().toLocaleString());
		domainTool.write(map);
	}

	public ReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}

}
